<template>
    <drawer-tabs-container>
        <div class="project-setting">
            <Button type="primary" @click="edit">{{title}}</Button>
            <TreeChart :json="data5" ref="TreeChart" @roload="getData" :isEdit="isEdit" :projectid="projectid"></TreeChart>
        </div>
    </drawer-tabs-container>
</template>

<style lang="scss" scoped>

</style>
<script>
    import DrawerTabsContainer from "../DrawerTabsContainer";
    import TreeChart from "./TreeChart";
    export default {
        name: 'menuModule',
        components: {DrawerTabsContainer,TreeChart},
        props: {
            projectid: {
                default: 0
            },
            canload: {
                type: Boolean,
                default: true
            },
        },
        data () {
            return {
                title:"维护模块",
                data5: {},
                isEdit:false
            }
        },
        mounted() {
           this.getData()
        },

        watch: {
            projectid() {
                if (this.loadYet) {
             
                }
            },
            canload(val) {
                if (val && !this.loadYet) {
                    this.loadYet = true;
              
                }
            }
        },

        methods: {
            // 维护模块
            edit(){
              this.isEdit =    !this.isEdit 
              if(this.isEdit){
                this.title = '完成'
              }else{
                this.title = '维护模块'
              }

            },
            /**
 * 把平铺的数组结构 ---> tree
 */
tranListToTreeData(arr) {
  const newArr = []
  // 1. 构建一个字典：能够快速根据id找到对象。
  const map = {}
  // {
  //   '01': {id:"01", pid:"",   "name":"老王",children: [] },
  //   '02': {id:"02", pid:"01", "name":"小张",children: [] },
  // }
  arr.forEach(item => {
    // 为了计算方便，统一添加children
    item.children = []
    // 构建一个字典
    const key = item.id
    map[key] = item
  })
 
  // 2. 对于arr中的每一项
  arr.forEach(item => {
    const parent = map[item.pid]
    if (parent) {
      //    如果它有父级，把当前对象添加父级元素的children中
      parent.children.push(item)
    } else {
      //    如果它没有父级（pid:''）,直接添加到newArr
      newArr.push(item)
    }
  })
 
  return newArr
},
arrayTreeAddLevel  (array, levelName = 'level', childrenName = 'children') {
    if (!Array.isArray(array)) return []
    const recursive = (array, level = 0) => {
        level++
        return array.map(v => {
            v[levelName] = level
            const child = v[childrenName]
            if (child && child.length) recursive(child, level)
            return v
        })
    }
    return recursive(array)
},

           
getData(){
  
    $A.apiAjax({
        url: 'project/step/list?projectid=' +this.projectid,
        
        complete: () => {
            
        },
        success: (res) => {
            let data = this.tranListToTreeData(res.data)
          let data1 = this.arrayTreeAddLevel(data)
            this.data5 = data1[0]
          
        }
    });
},

}
}
    
</script>
